<?php

class TVGS extends Zend_Db_Table_Abstract{
    protected $_name="bk_mau2_nanglucnhansu";
	function CountTVGSList($parameter){
		global $db;

		$where = " 1=1 ";
		$wherearray = Array();

		// tim theo ten to chuc
		if($parameter["FINDTYPE"]==1){
			$where = " AND tc.NAME LIKE ?";
			$wherearray[] = "%".$parameter["FINDFILTER"]."%";
		}

		// tim theo so dkkd
		if($parameter["FINDTYPE"]==2){
			$where = " AND tc.GPDKKD_SO LIKE ?";
			$wherearray[] = "%".$parameter["FINDFILTER"]."%";
		}

		// tim theo so dien thoai
		if($parameter["FINDTYPE"]==3){
			$where = " AND tc.DIENTHOAI LIKE ?";
			$wherearray[] = "%".$parameter["FINDFILTER"]."%";
		}

		// tim theo linh vuc
		if($parameter["ID_LV"] > 0){
			$where = " AND lv.ID_LV = ?";
			$wherearray[] = $parameter["ID_LV"];
		}

		$sql = "
			SELECT
				count(*) as CNT
			FROM
				bk_tochuc tc
				INNER JOIN bk_mau2 m2 ON tc.ID_TC = m2.ID_TC
				INNER JOIN bk_linhvuc lv ON m2.ID_LV = lv.ID_LV
			WHERE
				$where
		";

		$r = $db->query($sql,$wherearray);

		$r = $r->fetch();

		return $r["CNT"];
	}

	function SelectTVGSList($parameter, $page, $limit, $order){
		global $db;

		$where = " 1=1 ";
		$wherearray = Array();

		// tim theo ten to chuc
		if($parameter["FINDTYPE"]==1){
			$where = " AND tc.NAME LIKE ?";
			$wherearray[] = "%".$parameter["FINDFILTER"]."%";
		}

		// tim theo so dkkd
		if($parameter["FINDTYPE"]==2){
			$where = " AND tc.GPDKKD_SO LIKE ?";
			$wherearray[] = "%".$parameter["FINDFILTER"]."%";
		}

		// tim theo so dien thoai
		if($parameter["FINDTYPE"]==3){
			$where = " AND tc.DIENTHOAI LIKE ?";
			$wherearray[] = "%".$parameter["FINDFILTER"]."%";
		}

		// tim theo linh vuc
		if($parameter["ID_LV"] > 0){
			$where = " AND lv.ID_LV = ?";
			$wherearray[] = $parameter["ID_LV"];
		}

		// sap xep
		if($order != ""){
			$order = " ORDER BY ".$order;
		}else{
			$order = " ORDER BY m2.LASTCHANGE DESC";
		}

		// phan trang
		if($page > 0 && $limit > 0){
			$from = ($page-1) * $limit;
			$limit = " LIMIT $from,$limit";
		}else{
			$limit = "";
		}

		$sql = "
			SELECT
				m2.ID_BK2
				, tc.NAME as NAMETOCHUC
				, tc.TRUSO as TRUSOTOCHUC
				, tc.DIENTHOAI as DIENTHOAITOCHUC
				, tc.GPDKKD_SO
				, tc.GPDKKD_NGAY
				, lv.NAME as NAMELINHVUC
			FROM
				bk_tochuc tc
				INNER JOIN bk_mau2 m2 ON tc.ID_TC = m2.ID_TC
				INNER JOIN bk_linhvuc lv ON m2.ID_LV = lv.ID_LV
			WHERE
				$where
			$order
			$limit
		";

		$r = $db->query($sql,$wherearray);

		return $r->fetchAll();
	}

	function SelectTVGSByID($id_bm2){
		global $db;

		$sql = "
		SELECT
			m2.*
			, lv.NAME AS NAMELINHVUC
		FROM
			bk_mau2 m2
			INNER JOIN bk_linhvuc lv ON m2.ID_LV = lv.ID_LV
		WHERE
			m2.ID_BK2 = ?";

		return $db->query($sql,$id_bm2)->fetch();
	}

	function SelectLastTVGSByID_TC($id_tc){
		global $db;

		$sql = "
		SELECT
			m2.*
			, lv.NAME AS NAMELINHVUC
		FROM
			bk_mau2 m2
			INNER JOIN bk_linhvuc lv ON m2.ID_LV = lv.ID_LV
		WHERE
			m2.ID_TC = ? AND STATUS=-1
		ORDER BY m2.ID_BK2 DESC
		";

		return $db->query($sql,$id_tc)->fetch();
	}

	function SelectNangLucNhanSuListByID_BM2($id_bm2){
		global $db;

		$sql = "
			SELECT
				nl.*,lv.NAME as NAMELEVEL
			FROM
				bk_mau2_nanglucnhansu nl
				INNER JOIN bk_level lv ON nl.ID_LV = lv.ID_LV
			WHERE
				nl.ID_BK2 = ?
		";

		return $db->query($sql,$id_bm2)->fetchAll();
	}

	function SelectNangLucNhanSuByID_NLNS($id_nlns,$id_tc){
		global $db;

		$sql = "
			SELECT
				nl.*,lv.NAME as NAMELEVEL,DATE_FORMAT(nl.BAOHIEMXAHOI_NGAY,'%d/%m/%Y') as BAOHIEMXAHOI_NGAY_VN
			FROM
				bk_mau2_nanglucnhansu nl
				INNER JOIN bk_mau2 m2 ON nl.ID_BK2 = m2.ID_BK2
				INNER JOIN bk_level lv ON lv.ID_LV = nl.ID_LV
			WHERE
				nl.ID_NLNS = ?
				AND m2.ID_TC = ?
		";

		return $db->query($sql,Array($id_nlns,$id_tc))->fetch();
	}

	function SelectNangLucKhacListByID_BM2($id_bm2){
		global $db;

		$sql = "
			SELECT
				lv.*
				,nl.DIEM as DIEMNL
				,nl.ID_NLK
				,nl.GHICHU
			FROM
				bk_level lv
				LEFT JOIN bk_mau2_nangluckhac nl ON nl.ID_LV = lv.ID_LV AND nl.ID_BK2 = ?
			WHERE
				lv.TYPE = 'TVGS_KHAC'
		";

		return $db->query($sql,$id_bm2)->fetchAll();
	}

	function SelectDoanhThuListByID_BM2($id_bm2,$fromyear,$toyear){
		global $db;

		$sql = "
			SELECT
				'1' AS NOTHING
		";
		for($i=$fromyear;$i<=$toyear;$i++){
			$sql .=",sum(CASE WHEN dt.YEAR=$i THEN dt.DOANHTHU ELSE 0 END) as Y$i";
		}
		$sql .="
			FROM
				bk_tochuc_doanhthu dt
				INNER JOIN bk_mau2 m2 ON m2.ID_BK2 = dt.ID_BM
			WHERE
				m2.ID_BK2 = ?
				AND dt.TYPE = 'TVGS'
				AND dt.YEAR >= ? AND dt.YEAR <= ?
			GROUP BY m2.ID_BK2
		";

		return $db->query($sql,Array($id_bm2,$fromyear,$toyear))->fetch();
	}

	function CreateTVGSM2WithStatusIsDraft($parameter,$id_tc){
		global $db;
		$sql = "
			INSERT INTO bk_mau2(
				ID_LV
				,ID_TC
				,SONAMHOATDONG
				,DIEMDUAN
				,TONGDIEM
				,DIEM_DOANHTHU
				,DIEM_NANGLUCNHANSU
				,DIEM_SONAMHOATDONG
				,LASTCHANGE
				,STATUS
			)VALUES(
				?
				,?
				,?
				,?
				,?
				,?
				,?
				,?
				,?
				,?
			)
		";
		$db->query($sql,Array(
			$parameter["ID_LV"]
			,$id_tc
			,$parameter["SONAMHOATDONG"]
			,0
			,0
			,0
			,0
			,$parameter["DIEM_SONAMHOATDONG"]
			,date('Y-m-d H:i:s')
			,-1
		));

		return $db->lastInsertId();
	}

	function UpdateTVGSM2Buoc1($parameter,$id_bm2){
		global $db;
		$sql = "
			UPDATE bk_mau2
			SET
				ID_LV = ?
				,SONAMHOATDONG = ?
				,DIEM_SONAMHOATDONG = ?
				,LASTCHANGE = ?
			WHERE
				ID_BK2 = ?
		";
		$db->query($sql,Array(
			$parameter["ID_LV"]
			,$parameter["SONAMHOATDONG"]
			,$parameter["DIEM_SONAMHOATDONG"]
			,date('Y-m-d H:i:s')
			,$id_bm2
		));
	}

	function UpdateTVGSM2DIEM_DOANHTHU($parameter,$id_bm2){
		global $db;
		$sql = "
			UPDATE bk_mau2
			SET
				DIEM_DOANHTHU = ?
				,LASTCHANGE = ?
			WHERE
				ID_BK2 = ?
		";
		$db->query($sql,Array(
			$parameter["DIEM_DOANHTHU"]
			,date('Y-m-d H:i:s')
			,$id_bm2
		));
	}

	function UpdateTVGSM2DIEM_NANGLUCNHANSU($id_bm2){
		global $db;
		$sql = "
			UPDATE bk_mau2
			SET
				DIEM_NANGLUCNHANSU = (SELECT SUM(nl.DIEM) FROM bk_mau2_nanglucnhansu nl WHERE nl.STATUS IN (0,1,2) AND nl.ID_BK2 = ?)
				,LASTCHANGE = ?
			WHERE
				ID_BK2 = ?
		";
		$db->query($sql,Array(
			$id_bm2
			,date('Y-m-d H:i:s')
			,$id_bm2
		));
	}

	function CopyNangLucNhanSuListByID_BM2($id_bm2old,$id_bm2new){
		global $db;

		$sql = "
			INSERT INTO bk_mau2_nanglucnhansu(
				ID_BK2
				,HOTEN
				,CMND
				,NGHE
				,SOCHUNGCHIHANHNGHE
				,BAOHIEMXAHOI_SO
				,BAOHIEMXAHOI_NGAY
				,BAOHIEMXAHOI_NOICAP
				,ID_LV
				,DIEM
				,GHICHU
			)
			SELECT
				? as ID_BK2
				,HOTEN
				,CMND
				,NGHE
				,SOCHUNGCHIHANHNGHE
				,BAOHIEMXAHOI_SO
				,BAOHIEMXAHOI_NGAY
				,BAOHIEMXAHOI_NOICAP
				,ID_LV
				,DIEM
				,GHICHU
			FROM
				bk_mau2_nanglucnhansu
			WHERE
				ID_BK2 = ?
		";

		$db->query($sql,Array($id_bm2new,$id_bm2old));
	}

	function CopyNangLucKhacListByID_BM2($id_bm2old,$id_bm2new){
		global $db;

		$sql = "
			INSERT INTO bk_mau2_nangluckhac(
				ID_BK2
				,ID_LV
				,DIEM
			)
			SELECT
				? as ID_BK2
				,ID_LV
				,DIEM
			FROM
				bk_mau2_nangluckhac
			WHERE
				ID_BK2 = ?
		";

		$db->query($sql,Array($id_bm2new,$id_bm2old));
	}

	function CopyDoanhThuListByID_BM2($id_bm2old,$id_bm2new){
		global $db;

		$sql = "
			INSERT INTO bk_tochuc_doanhthu(
				ID_BM
				,ID_TC
				,YEAR
				,TYPE
				,DOANHTHU
			)
			SELECT
				? as ID_BM
				,ID_TC
				,YEAR
				,TYPE
				,DOANHTHU
			FROM
				bk_tochuc_doanhthu
			WHERE
				ID_BM = ?
				AND TYPE='TVGS'
		";

		$db->query($sql,Array($id_bm2new,$id_bm2old));
	}

	function CopyDuAnListByID_BM2($id_bm2old,$id_bm2new){
		global $db;

		$sql = "
			INSERT INTO bk_mau2_duan(
				ID_BK2,
                                NAMDUAN,
                                TENDUAN,
                                CAPDAUTU,
                                CHUDAUTU,
                                DAIDIENDAUTU,
                                LOAITYPE,
                                NHOMCAPCONGTRINH,
                                GHICHU_NHOMCAPCONGTRINH,
                                THONGTINDICHVU,
                                GHICHU_THONGTINDICHVU,
                                THONGTINBOSUNG,
                                GHICHU_THONGTINBOSUNG,
                                DIEM_DUAN
			)
			SELECT
				? as ID_BK2,
				NAMDUAN,
                                TENDUAN,
                                CAPDAUTU,
                                CHUDAUTU,
                                DAIDIENDAUTU,
                                LOAITYPE,
                                NHOMCAPCONGTRINH,
                                GHICHU_NHOMCAPCONGTRINH,
                                THONGTINDICHVU,
                                GHICHU_THONGTINDICHVU,
                                THONGTINBOSUNG,
                                GHICHU_THONGTINBOSUNG,
                                DIEM_DUAN
			FROM
				bk_mau2_duan
			WHERE
				ID_BK2 = ?
		";

		$db->query($sql,Array($id_bm2new,$id_bm2old));
	}

	// update nang luc nhan su voi dieu kien phai thuoc bieu mau dang lam
	function UpdateNangLucNhanSuByID_NLNS($parameter,$id_nlns,$id_bm2){
		global $db;
		
		$sql = "
			UPDATE bk_mau2_nanglucnhansu
			SET
				HOTEN = ?
				, CMND = ?
				, NGHE = ?
				, SOCHUNGCHIHANHNGHE = ?
				, BAOHIEMXAHOI_SO = ?
				, BAOHIEMXAHOI_NGAY = ?
				, BAOHIEMXAHOI_NOICAP = ?
				, ID_LV = ?
				, DIEM = ?
				, GHICHU = ?
				, STATUS = CASE 
					WHEN STATUS = 0 THEN 1 
					WHEN STATUS = 3 THEN 1 
					ELSE STATUS
					END
			WHERE
				ID_NLNS = ?
				AND ID_BK2 = ?
		";
		
		$db->query($sql,Array(
			$parameter["HOTEN"]
			,$parameter["CMND"]
			,$parameter["NGHE"]
			,$parameter["SOCHUNGCHIHANHNGHE"]
			,$parameter["BAOHIEMXAHOI_SO"]
			,$parameter["BAOHIEMXAHOI_NGAY"]
			,$parameter["BAOHIEMXAHOI_NOICAP"]
			,$parameter["ID_LV"]
			,$parameter["DIEM"]
			,$parameter["GHICHU"]
			,$id_nlns
			,$id_bm2
		));
	}

	// insert nang luc nhan su voi dieu kien phai thuoc bieu mau dang lam
	function InsertNangLucNhanSuByID_NLNS($parameter,$id_bm2){
		global $db;
		
		$sql = "
			INSERT bk_mau2_nanglucnhansu
			(
				ID_BK2
				,HOTEN
				,CMND
				,NGHE
				,SOCHUNGCHIHANHNGHE
				,BAOHIEMXAHOI_SO
				,BAOHIEMXAHOI_NGAY
				,BAOHIEMXAHOI_NOICAP
				,ID_LV
				,DIEM
				,GHICHU
				,STATUS
			)
			VALUES(?,?,?,?,?,?,?,?,?,?,?,?)
		";
		$db->query($sql,Array(
			$id_bm2
			,$parameter["HOTEN"]
			,$parameter["CMND"]
			,$parameter["NGHE"]
			,$parameter["SOCHUNGCHIHANHNGHE"]
			,$parameter["BAOHIEMXAHOI_SO"]
			,$parameter["BAOHIEMXAHOI_NGAY"]
			,$parameter["BAOHIEMXAHOI_NOICAP"]
			,$parameter["ID_LV"]
			,$parameter["DIEM"]
			,$parameter["GHICHU"]
			,2
		));

		return $db->lastInsertId();
	}
	function DeleteNangLucNhanSuByID_NLNS($id_nlns,$id_bm2,$deletelogic){
		global $db;

		// check xem co phai la them moi ko
		if($deletelogic==1){
			$sql = "
				UPDATE bk_mau2_nanglucnhansu
				SET
					STATUS = 3
				WHERE
					ID_NLNS = ?
					AND ID_BK2 = ?
			";
			
			$db->query($sql,Array(
				$id_nlns
				,$id_bm2
			));
		}else{
			$sql = "
				DELETE FROM bk_mau2_nanglucnhansu
				WHERE
					ID_NLNS = ?
					AND ID_BK2 = ?
			";
			
			$db->query($sql,Array(
				$id_nlns
				,$id_bm2
			));
		}
	}

	function UpdateNangLucKhacByID_LV($id_nlk,$id_lv,$diem,$ghichu,$id_bm2){
	
		global $db;

		$arrparam = Array();

		if($diem==0){
			$sql = "DELETE FROM bk_mau2_nangluckhac WHERE ID_LV = ? AND ID_BK2 = ?";
			$arrparam = Array($id_lv,$id_bm2);
		}else{
			if($id_nlk>0){
				$sql = "UPDATE bk_mau2_nangluckhac SET DIEM = ?,GHICHU = ? WHERE ID_LV = ? AND ID_BK2 = ?";
				$arrparam = Array($diem,$ghichu,$id_lv,$id_bm2);
			}else{
				$sql = "INSERT INTO bk_mau2_nangluckhac(ID_BK2,ID_LV,DIEM,GHICHU) VALUES(?,?,?,?)";
				$arrparam = Array($id_bm2,$id_lv,$diem,$ghichu);
			}
		}

		$db->query($sql,$arrparam);
	}

	function UpdateDoanhThuByID_BM2($id_bm2,$year,$doanhthu,$id_tc){
		global $db;

		$arrparam = Array();

		// check exists
		$sql = "SELECT ID_DT FROM bk_tochuc_doanhthu WHERE TYPE='TVGS' AND YEAR = ? AND ID_BM = ?";
		$r = $db->query($sql,Array($year,$id_bm2))->fetch();
		if($r["ID_DT"]>0){
			$sql = "UPDATE bk_tochuc_doanhthu SET DOANHTHU = ? WHERE ID_BM = ? AND YEAR = ? AND TYPE='TVGS'";
			$arrparam = Array($doanhthu,$id_bm2,$year);
		}else{
			$sql = "INSERT INTO bk_tochuc_doanhthu(ID_BM,ID_TC,YEAR,TYPE,DOANHTHU) VALUES(?,?,?,?,?)";
			$arrparam = Array($id_bm2,$id_tc,$year,'TVGS',$doanhthu);
		}

		$db->query($sql,$arrparam);
	}
        
        function SelectDuAnByID_DuAn($id_duan,$id_tc){
		global $db;

		$sql = "
			SELECT
				da.*
			FROM
				bk_mau2_duan da
				INNER JOIN bk_mau2 m2 ON da.ID_BK2 = m2.ID_BK2
			WHERE
				da.ID_DUAN = ?
				AND m2.ID_TC = ?
		";

		return $db->query($sql,Array($id_duan,$id_tc))->fetch();
        }
        
        function DeleteDuAnByID_DuAn($id_duan,$id_bm2,$deletelogic){
		global $db;

		// check xem co phai la them moi ko
		if($deletelogic==1){
			$sql = "
				UPDATE bk_mau2_duan
				SET
					STATUS = 3
				WHERE
					ID_DUAN = ?
					AND ID_BK2 = ?
			";
			
			$db->query($sql,Array(
				$id_duan
				,$id_bm2
			));
		}else{
			$sql = "
				DELETE FROM bk_mau2_duan
				WHERE
					ID_DUAN = ?
					AND ID_BK2 = ?
			";
			
			$db->query($sql,Array(
				$id_duan
				,$id_bm2
			));
		}
	}
        function UpdateTVGSM2DIEM_DUAN($id_bm2){
		global $db;
		$sql = "
			UPDATE bk_mau2 bm2
			SET
				bm2.DIEMDUAN= (SELECT SUM(da.DIEM_DUAN) FROM bk_mau2_duan da WHERE da.STATUS IN (0,1,2) AND da.ID_BK2 = ?)
				,bm2.LASTCHANGE = ?
			WHERE
				bm2.ID_BK2 = ?
		";
		$db->query($sql,Array(
			$id_bm2
			,date('Y-m-d H:i:s')
			,$id_bm2
		));
	}
        function UpdateDuAnByID_DUAN($parameter,$id_duan,$id_bm2){
		global $db;
		
		$sql = "
			UPDATE bk_mau2_duan
			SET
				NAMDUAN =? ,
                                TENDUAN =? ,
                                CAPDAUTU =? ,
                                CHUDAUTU =? ,
                                DAIDIENDAUTU =? ,
                                LOAITYPE =? ,
                                NHOMCAPCONGTRINH =? ,
                                GHICHU_NHOMCAPCONGTRINH =? ,
                                THONGTINDICHVU =? ,
                                GHICHU_THONGTINDICHVU =? ,
                                THONGTINBOSUNG =? ,
                                GHICHU_THONGTINBOSUNG =? ,
                                DIEM_DUAN =? ,
                                STATUS = CASE 
					WHEN STATUS = 0 THEN 1 
					WHEN STATUS = 3 THEN 1 
					ELSE STATUS
					END
			WHERE
				ID_DUAN = ?
				AND ID_BK2 = ?
		";
		
		$db->query($sql,Array(
			$parameter["NAMDUAN"],
                        $parameter["TENDUAN"],
                        $parameter["CAPDAUTU"],
                        $parameter["CHUDAUTU"],
                        $parameter["DAIDIENDAUTU"],
                        $parameter["LOAITYPE"],
                        $parameter["NHOMCAPCONGTRINH"],
                        $parameter["GHICHU_NHOMCAPCONGTRINH"],
                        $parameter["THONGTINDICHVU"],
                        $parameter["GHICHU_THONGTINDICHVU"],
                        $parameter["THONGTINBOSUNG"],
                        $parameter["GHICHU_THONGTINBOSUNG"],
                        $parameter["DIEM_DUAN"],
			$id_duan,
			$id_bm2
		));
	}
        function InsertDuAnByID_DUAN($parameter,$id_bm2){
		global $db;
		
		$sql = "
			INSERT bk_mau2_duan
			(
				ID_BK2,
				NAMDUAN,
                                TENDUAN,
                                CAPDAUTU,
                                CHUDAUTU,
                                DAIDIENDAUTU,
                                LOAITYPE,
                                NHOMCAPCONGTRINH,
                                GHICHU_NHOMCAPCONGTRINH,
                                THONGTINDICHVU,
                                GHICHU_THONGTINDICHVU,
                                THONGTINBOSUNG,
                                GHICHU_THONGTINBOSUNG,
                                DIEM_DUAN,
                                STATUS
			)
			VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
		";
		$db->query($sql,Array(
			$id_bm2,			
			$parameter["NAMDUAN"],
                        $parameter["TENDUAN"],
                        $parameter["CAPDAUTU"],
                        $parameter["CHUDAUTU"],
                        $parameter["DAIDIENDAUTU"],
                        $parameter["LOAITYPE"],
                        $parameter["NHOMCAPCONGTRINH"],
                        $parameter["GHICHU_NHOMCAPCONGTRINH"],
                        $parameter["THONGTINDICHVU"],
                        $parameter["GHICHU_THONGTINDICHVU"],
                        $parameter["THONGTINBOSUNG"],
                        $parameter["GHICHU_THONGTINBOSUNG"],
                        $parameter["DIEM_DUAN"],
                        2
		));

		return $db->lastInsertId();
	}
        function SelectDuAnListByID_BM2($id_bm2){
		global $db;

		$sql = "
			SELECT
				da.*
			FROM
				bk_mau2_duan da
			WHERE
				da.ID_BK2 = ?
		";

		return $db->query($sql,$id_bm2)->fetchAll();
	}
        function SelectTVGSThamDinhById($id_bm2)
        {
            global $db;

		$sql = "
			SELECT
				td.*
			FROM
				tvgs_thamdinh td
			WHERE
				td.ID_BK2 = ?
		";

		return $db->query($sql,$id_bm2)->fetchAll();
        }
        function InsertTVGSThamDinhBuoc1($parameter,$id_bm2)
        {
            global $db;
		
		$sql = "
			INSERT tvgs_thamdinh
			(
				ID_BK2,
				BUOC1,
                                GHICHU_BUOC1
			)
			VALUES(?,?,?)
		";
		$db->query($sql,Array(
			$id_bm2,			
			$parameter["BUOC1"],
                        $parameter["GHICHU_BUOC1"],
		));

		return $db->lastInsertId();
        }
        function UpdateTVGSThamDinhBuoc1($parameter,$id)
        {
            global $db;
		$sql = "
			UPDATE tvgs_thamdinh
			SET
				ID_BK2 =?,
				BUOC1=?,
                                GHICHU_BUOC1=?
			WHERE
				ID_THAMDINH = ?
		";
		$db->query($sql,Array(
			$parameter["ID_BK2"],			
			$parameter["BUOC1"],
                        $parameter["GHICHU_BUOC1"],
                        $id,
		));
        }
        
        function InsertTVGSThamDinhBuoc2($parameter,$id_bm2)
        {
            global $db;
		
		$sql = "
			INSERT tvgs_thamdinh
			(
				ID_BK2,
				BUOC2,
                                GHICHU_BUOC2
			)
			VALUES(?,?,?)
		";
		$db->query($sql,Array(
			$id_bm2,			
			$parameter["BUOC2"],
                        $parameter["GHICHU_BUOC2"],
		));

		return $db->lastInsertId();
        }
        function UpdateTVGSThamDinhBuoc2($parameter,$id)
        {
            global $db;
		$sql = "
			UPDATE tvgs_thamdinh
			SET
				ID_BK2 =?,
				BUOC2=?,
                                GHICHU_BUOC2=?
			WHERE
				ID_THAMDINH = ?
		";
		$db->query($sql,Array(
			$parameter["ID_BK2"],			
			$parameter["BUOC2"],
                        $parameter["GHICHU_BUOC2"],
                        $id,
		));
        }
         function InsertTVGSThamDinhBuoc3($parameter,$id_bm2)
        {
            global $db;
		
		$sql = "
			INSERT tvgs_thamdinh
			(
				ID_BK2,
				BUOC3,
                                GHICHU_BUOC3
			)
			VALUES(?,?,?)
		";
		$db->query($sql,Array(
			$id_bm2,			
			$parameter["BUOC3"],
                        $parameter["GHICHU_BUOC3"],
		));

		return $db->lastInsertId();
        }
        function UpdateTVGSThamDinhBuoc3($parameter,$id)
        {
            global $db;
		$sql = "
			UPDATE tvgs_thamdinh
			SET
				ID_BK2 =?,
				BUOC3=?,
                                GHICHU_BUOC3=?
			WHERE
				ID_THAMDINH = ?
		";
		$db->query($sql,Array(
			$parameter["ID_BK2"],			
			$parameter["BUOC3"],
                        $parameter["GHICHU_BUOC3"],
                        $id,
		));
        }
         function InsertTVGSThamDinhBuoc4($parameter,$id_bm2)
        {
            global $db;
		
		$sql = "
			INSERT tvgs_thamdinh
			(
				ID_BK2,
				BUOC4,
                                GHICHU_BUOC4,
                                IS_CONGBO
			)
			VALUES(?,?,?,?)
		";
		$db->query($sql,Array(
			$id_bm2,			
			$parameter["BUOC4"],
                        $parameter["GHICHU_BUOC4"],
                    $parameter["IS_CONGBO"]
		));

		return $db->lastInsertId();
        }
        function UpdateTVGSThamDinhBuoc4($parameter,$id)
        {
            global $db;
		$sql = "
			UPDATE tvgs_thamdinh
			SET
				ID_BK2 =?,
				BUOC4=?,
                                GHICHU_BUOC4=?,
                                IS_CONGBO=?
			WHERE
				ID_THAMDINH = ?
		";
		$db->query($sql,Array(
			$parameter["ID_BK2"],			
			$parameter["BUOC4"],
                        $parameter["GHICHU_BUOC4"],
                        $parameter["IS_CONGBO"],
                        $id,
		));
        }
        function InsertTVGSKhongthamdinh($parameter,$id_bm2)
        {
            global $db;
		
		$sql = "
			INSERT tvgs_thamdinh
			(
				ID_BK2,
				IS_KHONGTHAMDINH
			)
			VALUES(?,?)
		";
		$db->query($sql,Array(
			$id_bm2,			
			$parameter["IS_KHONGTHAMDINH"]
		));

		return $db->lastInsertId();
        }
        function UpdateTVGSKhongthamdinh($parameter,$id)
        {
            global $db;
		$sql = "
			UPDATE tvgs_thamdinh
			SET
				ID_BK2 =?,
				IS_KHONGTHAMDINH=?
			WHERE
				ID_THAMDINH = ?
		";
		$db->query($sql,Array(
			$parameter["ID_BK2"],			
			$parameter["IS_KHONGTHAMDINH"],
                        $id
		));
        }
        function UpdateTVGSKetQua($parameter,$id)
        {
            global $db;
		$sql = "
			UPDATE tvgs_thamdinh
			SET
				ID_BK2 =?,
				KETQUA=?
			WHERE
				ID_THAMDINH = ?
		";
		$db->query($sql,Array(
			$parameter["ID_BK2"],			
			$parameter["KETQUA"],
                        $id
		));
        }
        
}